<?php
// +----------------------------------------------------------------------
// | zhanshop-device / Volume.php    [ 2024/1/19 下午9:40 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2024 zhangqiquan All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhangqiquan <768617998@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace extend\docker;

class Volume extends EngineApi
{
    /**
     * 获取数据卷列表
     * @return array|mixed
     */
    public function getList()
    {
        $volumes = $this->request("/volumes");
        $volumes['body'] = strtolower($volumes['body'] ?? "");
        $volumes = json_decode($volumes['body'], true);
        return $volumes ?? [];
    }

    /**
     * 创建数据卷
     * @param $name
     * @param $driver
     * @param $options
     * @param $labels
     * @param $clusterVolumeSpec
     * @return array|mixed
     * @throws \Exception
     */
    public function create($name, $driver = "local", $options = null, $labels = null, $clusterVolumeSpec = null)
    {
        $data = [];
        $data['Name'] = $name;
        $data['Driver'] = $driver;

        $volume = $this->request("/volumes/create", "POST", $data);
        $volume['body'] = strtolower($volume['body'] ?? "");
        $volume = json_decode($volume['body'], true);
        return $volume ?? [];
    }

    /**
     * 删除
     * @param string $name
     * @return array|mixed
     * @throws \Exception
     */
    public function delete(string $name)
    {
        $this->request("/volumes/".$name, "DELETE");
        return [];
    }
}